package com.mls.fivedistrict.controller.backend;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.exception.ParamException;
import com.mls.fivedistrict.pojo.News;
import com.mls.fivedistrict.server.NewsService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Slf4j
@RestController("backendNewsController")
@RequestMapping("/api/manager")
public class NewsController {
    @Autowired
    NewsService newsService;

    @GetMapping("/v1/newses")
    public ServiceResponse getNewsList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List newsList = newsService.selectList(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo<News>(newsList, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }
    @GetMapping("/v1/news/{newsId}")
    public ServiceResponse getNews(@PathVariable Long newsId){
        Optional.of(newsId).orElseThrow(() -> new ParamException("newsId不能为空"));
        News news = newsService.selectDetail(newsId);
        if(null!=news){
            return ServiceResponse.createSuccessResponse(news);
        }else{
            return ServiceResponse.createErrorResponse("查无此新闻");
        }
    }
    @PutMapping("/v1/news/{newsId}")
    public ServiceResponse updateNewsById(@PathVariable Long newsId, @RequestBody News news) {
        Optional.of(newsId).orElseThrow(() -> new ParamException("newsId不能为空"));
        Optional.of(news).orElseThrow(() -> new ParamException("news不能为空"));
        news.setId(newsId);
        boolean flag = newsService.updateById(news);
        if (flag) {
            return ServiceResponse.createSuccessResponse("编辑成功");
        } else {
            return ServiceResponse.createErrorResponse("编辑失败");
        }

    }

    @PostMapping("/v1/news")
    public ServiceResponse addNews(@RequestBody News news) {
        //1所有的添加操作统一校验
        BeanValidator.check(news);
        log.info("digest{}", news.getDigest());
        //2打印统一使用log.info()
        //3.要根据sql的返回值判断是否成功
        boolean insert = newsService.insert(news);
        if (insert) {
            return ServiceResponse.createSuccessResponse("新闻添加成功");
        } else {
            return ServiceResponse.createErrorResponse("新闻添加失败");
        }

    }

    @DeleteMapping("/v1/news/{newsId}")
    public ServiceResponse deleteNewsById(@PathVariable Long newsId) {
        Optional.of(newsId).orElseThrow(() -> new ParamException("新闻id不能为空"));
        News news = new News();
        news.setId(newsId);
        news.setDelete(true);
        boolean flag = newsService.updateById(news);
        if (flag) {
            return ServiceResponse.createSuccessResponse("删除成功");
        } else{
            return ServiceResponse.createErrorResponse("删除失败");
        }

    }
}
